/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain (a) copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry.ohos.core;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.bundle.AbilityInfo;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;
import ohos.utils.net.Uri;

import org.jetbrains.annotations.ApiStatus;

import io.sentry.Sentry;
import io.sentry.SentryLevel;

/**
 * SentryInitProvider
 *
 * @since 2021-06-01
 */
@ApiStatus.Internal
public final class SentryInitProvider extends Ability {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        OhosLogger logger = new OhosLogger();
        final Context context = getContext();

        if (context == null) {
            logger.log(SentryLevel.FATAL, "App. Context from DataAbility is null");
            return;
        }
        if (ManifestMetadataReader.isAutoInit(context, logger)) {
            SentryOhos.init(context, logger);
        }
    }

    @Override
    public void shutdown() {
        Sentry.close();
    }

    @Override
    public void onInitialized(AbilityInfo abilityInfo) {
        // bundleName is expected to be prepended. See config.json
        String name = abilityInfo.getBundleName()+"."+abilityInfo.getClassName();
        if (SentryInitProvider.class.getName().equals(name)) {
            throw new IllegalStateException(
                "An bundleName is required to fulfill the config placeholder.");
        }
        super.onInitialized(abilityInfo);
    }

    @Override
    public ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) {
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public int insert(Uri uri, ValuesBucket value) {
        return super.insert(uri, value);
    }

    @Override
    public int delete(Uri uri, DataAbilityPredicates predicates) {
        return 0;
    }

    @Override
    public int update(Uri uri, ValuesBucket value, DataAbilityPredicates predicates) {
        return 0;
    }
}
